# ========================
# T-101 Telegram Bot
#           Home Assistant
# ========================

# https://developers.home-assistant.io/docs/api/rest/

'''
{"entity_id":"sensor.a4c138bd3c77_fe95_temperature","state":"27.4","attributes":{"rssi":-64,"battery":100,"humidity":46.9,"temperature":27.4,"unit_of_measurement":"°C","device_class":"temperature","friendly_name":"Therm_gest_temp"},"last_changed":"2024-06-11T16:38:52.676784+00:00","last_updated":"2024-06-11T16:39:58.767685+00:00","context":{"id":"01J042QBHFK1W89EWGCAEVWDMH","parent_id":null,"user_id":null}}
'''

def ha_get_temp(sensor):

    import json
    from requests import get
    from core import config

    url = 'http://localhost:8123/api/states/'
    headers = {'Authorization': 'Bearer ' + config.ha_token}

    response = get(url + sensor, headers=headers)
    result = json.loads(response.text)
    temperat = str(result['attributes']['temperature'])
    humidity = str(result['attributes']['humidity'])

    return temperat + ' C, ' + humidity + ' %'



def getHA():

    # Температура и влажность в гостиной
    gest = ha_get_temp('sensor.a4c138bd3c77_fe95_temperature')

    # Температура в спальне
    rest = ha_get_temp('sensor.therm_rest_a4c138f7eae7_fe95_temperature')

    # Температура в детской
    kids = ha_get_temp('sensor.a4c1380e6157_fe95_temperature')

    # Температура на кухне
    cook = ha_get_temp('sensor.a4c1381a3769_fe95_temperature')

    msg = '\
Температура и влажность:\n \
  в гостиной: ' + gest + '\n \
  в спальне: '  + rest + '\n \
  в детской: '  + kids + '\n \
  на кухне: '   + cook

    return msg

